<template>
  <div class="register">
    <el-container>
      <el-card class="form-card">
        <el-form :model="registerForm" ref="registerForm" :rules="rules">
          <h3>注册账户</h3>
          <el-form-item prop="userName">
            <el-input placeholder="请输入用户名" v-model="registerForm.userName"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" v-model="registerForm.userPassword" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('registerForm')">立即创建</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">返回登录界面</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-container>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        registerForm: {
          userName: '',
          userPassword: ''
        },
        rules: {
          userName: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ],
          userPassword: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const _this = this
            axios.post('http://localhost:2020/register',this.registerForm).then(function (res) {
                if(res.data.status === 200) {
                  _this.$message({
                    showClose: true,
                    message: res.data.msg,
                    type: 'success',
                    center: true
                  });
                  _this.$router.push('/');
                } else {
                  _this.$confirm(res.data.msg, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                  })
                }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      login() {
        this.$router.push("/")
      }
    }
  }
</script>

<style scoped>

  .el-container {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #b598a1;
  }
  h3 {
    margin: 16px;
  }
  .form-card {
    width: 400px;
    background: #e0c8d1;
    border: none;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,.5);
  }
  .el-button {
    width: 100%;
  }
</style>